public class Tester {
	
	private int ask;
	private int shout;
	
	public void go() throws InterruptedException
	{
		Channel c = new Channel();
		
		Thread[] threads = new Thread[100];
		
		for(int i = 0; i < 100; i++)
		{
			(threads[i] = (new TestThread(c, this))).start();
		}
		
		Thread.sleep(5000);
		
		System.out.println("Ask count: " + c.getAskCount());
		System.out.println("Shout count: " + c.getShoutCount());
		System.out.println("Number of asks: " + ask);
		System.out.println("Number of shouts: " + shout);
	}
	/*
	 * Test channel
	 */
	public static void main(String[] args) throws InterruptedException
	{
		Tester t = new Tester();
		
		t.go();
	}

	private class TestThread extends Thread
	{
		Channel c;
		Tester t;
		public TestThread(Channel c, Tester t)
		{
			this.t = t;
			this.c = c;
		}
		
		public void run()
		{
			if(Math.random() > 0.5)
			{
				System.out.println("Ask!");
				t.incrAsk();
				c.ask();
				t.decrAsk();
			}
			else
			{
				System.out.println("Shout!");
				t.incrShout();
				c.shout();
				t.decrShout();
			}
		}
	}

	public synchronized void incrAsk() {
		ask++;
	}
	public synchronized void decrAsk() {
		ask--;
	}
	public synchronized void incrShout() {
		shout++;
	}
	public synchronized void decrShout() {
		shout--;
	}
	
	public synchronized int getShout() {
		return shout;
	}
	
	public synchronized int getAsk() {
		return ask;
	}

}
